﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Furion.Schedule;

namespace Abc.Core.EventBus;

/// <summary>
/// 清理数据库job
/// </summary>
[JobDetail("job_EventBusClearDbJob", concurrent: false, "清理数据库job")]
//文档：26.1.5.6 作业触发器特性及自定义
[Daily(TriggerId = "trigger_EventBusClearDbJob", StartNow = true)]
public class EventBusClearDbJob : IJob
{
    private readonly ISqlSugarClient _db;

    public EventBusClearDbJob(ISqlSugarClient db)
    {
        _db = db;
    }

    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        var option = AppEx.GetConfig<EventBusOptions>();
        //判断是否开启了事件总线
        if (option.IsEnableEventBus == false)
        {
            await Task.CompletedTask;
            return;
        }
        var _logger = AppEx.GetSeriLogger(logFloderName: $"EventBus\\ClearDbJob\\");
        _logger.Debug($"清理数据库job  任务");
        try
        {
            //清理一个月之前的数据
            var mintime = DateTimeOffset.Now.AddDays(-option.EventDataSaveDay);
            //await _db.Deleteable<SysEventSourceStorer>().Where(it => it.CreationTime < mintime).ExecuteCommandAsync();

        }
        catch (Exception ex)
        {
            _logger.Error(ex.ToStringEx());
        }
        finally
        {
        }

        await Task.CompletedTask;
    }
}
